package yxy.game.pm2.module.union.player.tech;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

import org.joda.time.DateTime;
import yxy.apple.util.ActiveUtil;
import yxy.cherry.battle.module.PropertyData;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.unionskill;
import yxy.cherry.data.script.fomula;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.CurrencyType;

public class UnionPlayerTech {

    // bean===========================================================================
    private int resetTimes;// 重置次数
    private ArrayList<UnionTech> techList = new ArrayList<>();
    private DateTime resetTime;//重置时间

    public DateTime getResetTime() {
        return resetTime;
    }

    public void setResetTime(DateTime resetTime) {
        this.resetTime = resetTime;
    }

    public int getResetTimes() {
        return resetTimes;
    }

    public void setResetTimes(int resetTimes) {
        this.resetTimes = resetTimes;
    }

    public ArrayList<UnionTech> getTechList() {
        return techList;
    }

    public void setTechList(ArrayList<UnionTech> techList) {
        this.techList = techList;
    }

    // func===========================================================================

    /**
     * 判断科技已经最大级
     */
    public boolean max(int dataId) {
        for (UnionTech tech : getTechList()) {
            if (tech.getDataId() == dataId) {
                return tech.getLevel() >= tech.data().getLvmax();
            }
        }
        return true;
    }

    /**
     * 升级
     */
    public UnionTech upgrade(int dataId) {
        for (UnionTech tech : getTechList()) {
            if (tech.getDataId() == dataId) {
                tech.upgrade();
                return tech;
            }
        }
        return null;
    }

    /**
     * 获得消费额
     */
    public Collection<Currency> cost(int dataId) {
        for (UnionTech tech : getTechList()) {
            if (tech.getDataId() == dataId) {
                return tech.cost();
            }
        }
        return null;
    }

    /**
     * 获取加成属性
     */
    public PropertyData property(int herotype) {
        PropertyData propertyData = new PropertyData();
        for (UnionTech tech : getTechList()) {
            if (tech.data().getType() == herotype) {
                propertyData.addAllValue(tech.property());
                break;
            }
        }
        return propertyData;
    }

    public Currency resetcost() {
        DateTime dateTime = DateTime.now().withTimeAtStartOfDay();
        DateTime monthBegin = ActiveUtil.getMonthBegin(getResetTime());
        int number = getResetTimes() == 0 && ActiveUtil.isSameDay(dateTime, monthBegin) ? 0 : fomula.联盟技能重置花费元宝数额.intValue();//
        return Currency.create(CurrencyType.CT_元宝, 0, number);
    }

    public Collection<Currency> resetback(int dataId) {
        for (UnionTech tech : getTechList()) {
            if (tech.getDataId() == dataId) {
                return tech.resetback();
            }
        }
        return null;
    }

    public UnionTech reset(int dataId) {
        for (UnionTech tech : getTechList()) {
            if (tech.getDataId() == dataId) {
                tech.reset();
                resetTimes++;
                return tech;
            }
        }
        return null;
    }

    public void update() {
    }

    static public UnionPlayerTech create() {
        Map<String, unionskill> datas = DataCenter.getAllData(unionskill.class);

        UnionPlayerTech playerTech = new UnionPlayerTech();
        for (unionskill data : datas.values()) {
            UnionTech tech = new UnionTech();
            tech.setDataId(data.getId());
            playerTech.getTechList().add(tech);
        }
        return playerTech;
    }

}
